package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.Person;
import ModelLayer.Customer;
import ModelLayer.Employee;

/**
 * Write a description of class PersonUI here.
 * 
* @author Team Black
 * @version (a version number or a date)
 */
public class PersonUI
{
    private PersonCtr personCtr;
    private Reader reader;
    private Customer customer;
    private MainMenuUI mainMenu;
    
    public PersonUI()
    {
        
    }
    /**
     * prints the main menu and takes an input from the user
     */
    public void startPerson()
    {
        boolean exit = false;
        reader = new Reader();
        personCtr = new PersonCtr();
        
        mainMenu = new MainMenuUI();
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeMainMenu();
            if(choise == 1)
            {
                createCustomers();
            }//end if
            else
            {
                    if(choise == 2)
                    { 
                        searchCustomers();
                    }//end if
                        else
                        {
                            if(choise == 3)
                            {
                                editCustomers();
                            }//end if
                                    if(choise ==4)
                                    {
                                        listAllCustomers();
                                    }//end if
                                    else
                                    {
                                        if(choise == 5)
                                        {
                                            deleteCustomer();
                                        }//end if
                                        else
                                        {
                                            if(choise == 6)
                                            {
                                               createEmployee();
                                            }//end if
                                                if(choise == 7)
                                                {
                                                    searchEmployee();
                                                }//end if
                                                else
                                                {
                                                    if(choise == 8)
                                                    {
                                                        editEmployee();
                                                    }//end if
                                                    else{
                                                        if(choise == 9)
                                                        {
                                                            listAllEmployee();
                                                        }//end if
                                                        else
                                                        {
                                                            if(choise == 10)
                                                            {
                                                                deleteEmployee();
                                                            }//end if
                                                                else
                                                                {
                                                                    if(choise == 11)
                                                                    {
                                                                         exit = true;
                                                                         mainMenu.mainMenuStart();
                                                                    }
                                                                    else 
                                                                    {
                                                                        System.out.print("forkert input, prøv igen.");
                                                                        pause();
                                                                    }//end else
                                                                }//end menu 
                                     }//end else
                                  }//end else
                               }//end else
                            }
                        }//end else
                    }// end else
             }//end else
        }//end while
    }//end start
   
   /**
    * prints the main menu 
    */
   private int writeMainMenu()
   {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Kunde Bog ***");
            System.out.println(" (1) Opret Kunde");
            System.out.println(" (2) Søg kunde");
            System.out.println(" (3) Rediger Kunde");
            System.out.println(" (4) List alle kunder");
            System.out.println(" (5) Slet Kunde");
            System.out.println(" (6) Opret Ekspedient");
            System.out.println(" (7) Søg Ekspedient");
            System.out.println(" (8) Rediger Ekspedient");
            System.out.println(" (9) List alle Ekspedienter");
            System.out.println(" (10) Slet Ekspedient");
            System.out.println(" (11) Tilbage");
            
            System.out.print("\n Tryk for at komme videre: ");

            int choise = 0;
            try{
                choise = keyboard.nextInt();
            }
            catch(Exception e){
                 System.out.println("\nInput skal være et tal, prøv igen.");
                 pause();
                 startPerson();
            }
            return choise;
       }
   
       
   /**
    * creates an object of Customer
    */    
   private void createCustomers()
   {
            System.out.println();
            System.out.println(" Opret ny Kunde: ");
            String _name = reader.inputName();
            String _adress = reader.inputAddress();
            String _zipCode = reader.inputPostalCode();
            String _phoneNr = reader.inputPhone();
            int _discount = reader.inputDiscount();
            
            personCtr.createCustomer(_name, _adress, _zipCode, _phoneNr, _discount);
            Customer customer = personCtr.searchCustomerByName(_name);
       
            System.out.println();
            System.out.println(" Kunde " + customer.getName() + " ID: " + customer.getId() + " er blevet oprettet!");
            pause();
   }
   
   /**
    * searches for an object of Customer
    */
   private void searchCustomers()
   {
       System.out.println("Søge efter Ansat ud fra navn eller id?");
       boolean exit = false;
       while(!exit)
       {
           int choise = writeSearchMenu();
           if(choise == 1)
           {
                   String _name = reader.inputName();
                   
                   Customer newCustomer = personCtr.searchCustomerByName(_name);
                   
                   System.out.println("Info om " +newCustomer.getName());
                   System.out.println("ID            : " +newCustomer.getId());
                   System.out.println("Adresse       : " +newCustomer.getAdress());
                   System.out.println("Post Nummer   : " +newCustomer.getZipCode());
                   System.out.println("Telefon Nummer: " +newCustomer.getPhoneNr());
                   System.out.println("Rabat aftale  : " +newCustomer.getDiscount());
                   pause();
                   exit = true;               
           }//end if
           else
           {
               if(choise == 2)
               {
                   int _id = reader.inputId();
                   
                   Customer newCustomer = personCtr.searchCustomerById(_id);
                   
                   System.out.println("Info om " +newCustomer.getName());
                   System.out.println("ID            : " +newCustomer.getId());
                   System.out.println("Adresse       : " +newCustomer.getAdress());
                   System.out.println("Post Nummer   : " +newCustomer.getZipCode());
                   System.out.println("Telefon Nummer: " +newCustomer.getPhoneNr());
                   System.out.println("Rabat aftale  : " +newCustomer.getDiscount());
                   pause();
                   exit = true;      
               }//end If
               else
               {
                   if(choise == 3)
                   {
                       exit = true;
                   }//end if
                
           }//end if
        }//end if   
       }//end while
   }
   
   /**
    * edits an object of Customer
    */
   private void editCustomers()
   {
       System.out.println("Opdater kunde");
       //System.out.println("søg ekspedient ud fra navn eller id?");
       boolean exit = false;
       while(!exit)
       {
           int choise = writeSearchMenu();
           Customer currCustomer;
           int _id = 0;
           if(choise == 1)
           {
                   String _name = reader.inputName();
                   currCustomer = personCtr.searchCustomerByName(_name);
                   _id = currCustomer.getId();                        
           }//end if
           else if (choise == 2)
               {
                   _id = reader.inputId();
                   currCustomer = personCtr.searchCustomerById(_id);
                   }//end If
                   
           else if(choise == 3)
                   {
                       exit = true;
                   }//end if
           
           if(choise!=3)
           {
                String _name = reader.inputName();
                String _adress = reader.inputAddress();
                String _zipCode = reader.inputPostalCode();
                String _phoneNr = reader.inputPhone();
                String _salaryAccount = reader.inputSalaryAccount();
                int _discount = reader.inputDiscount();

                personCtr.updateCustomer(_id, _name, _adress, _zipCode, _phoneNr, _discount);
           
                System.out.println(_name + " er blevet opdateret");
                exit = true;
           }
        }//end if   
   }
   /**
    * lists all objects of Customer
    */
   private String listAllCustomers()
   {
       return personCtr.returnAllCustomers();
   }
   /**
    * deletes an object of Customer
    */
   private void deleteCustomer()
   {
       System.out.println("Slet kunde ud fra navn eller id?");
       boolean exit = false;
       while(!exit)
       {
           int choise = writeDeleteMenu();
           if(choise == 1)
           {
               int _id = reader.inputId();
               personCtr.deleteCustomer(_id);
               System.out.println("Kunde slettet");
               exit = true;
           }//end if
           else
           {
               if(choise == 2)
               {
                   String _name = reader.inputName();
                   personCtr.deleteCustomer(_name);
                   System.out.println("Kunde slettet");
                   exit = true;
               }//end If
               else
               {
                   if(choise == 3)
                   {
                       exit = true;
                   }//end if
                
           }//end if
        }//end if   
       }//end while
       
   }
   /**
    * creates an object of Employee
    */
   public void createEmployee()
   {
            System.out.println();
            System.out.println(" Opret ny Ekspedien: ");

            String _name = reader.inputName();
            String _adress = reader.inputAddress();
            String _zipCode = reader.inputPostalCode();
            String _phoneNr = reader.inputPhone();
            int _discount = reader.inputDiscount();
            String _salaryAccount = reader.inputSalaryAccount();
            
            personCtr.createEmployee(_name, _adress, _zipCode, _phoneNr, _salaryAccount, _discount);
            Employee employee = personCtr.searchEmployeeByName(_name);
            
            System.out.println();
            System.out.println(" Ansat " + employee.getName() + " ID: " + employee.getId() + " er blevet oprettet!");
            pause();
            startPerson();
   }
   /**
    * Search an object of Employee
    */
   public void searchEmployee()
   {
       System.out.println("Søge efter Ansat ud fra navn eller id?");
       boolean exit = false;
       while(!exit)
       {
           int choise = writeSearchMenu();
           if(choise == 1)
           {
               String _name = reader.inputName();
               Employee newEmployee = personCtr.searchEmployeeByName(_name);
               System.out.println("Info om " + newEmployee.getName());
               System.out.println("ID            : " +newEmployee.getId());
               System.out.println("Adresse       : " +newEmployee.getAdress());
               System.out.println("Post Nummer   : " +newEmployee.getZipCode());
               System.out.println("Telefon Nummer: " +newEmployee.getPhoneNr());
               System.out.println("Rabat aftale  : " +newEmployee.getDiscount());
               exit = true;
               pause();              
           }//end if
           else
           {
               if(choise == 2)
               {
                   int _id = reader.inputId();
                   Employee newEmployee = personCtr.searchEmployeeById(_id);
                   System.out.println("Info om " + newEmployee.getName());
                   System.out.println("ID            : " +newEmployee.getId());
                   System.out.println("Adresse       : " +newEmployee.getAdress());
                   System.out.println("Post Nummer   : " +newEmployee.getZipCode());
                   System.out.println("Telefon Nummer: " +newEmployee.getPhoneNr());
                   System.out.println("Rabat aftale  : " +newEmployee.getDiscount());
                   exit = true;
                   pause();
               }//end If
               else
               {
                   if(choise == 3)
                   {
                       exit = true;
                   }//end if
           }//end if
        }//end if   
       }//end while
       
   }
   /**
    * edit an object of Employee
    */
   public void editEmployee()
   {
       System.out.println("Opdater ansat");
       //System.out.println("søg ekspedient ud fra navn eller id?");
       boolean exit = false;
       while(!exit)
       {
           int choise = writeSearchMenu();
           Employee currEmployee;
           int _id = 0;
           
            if(choise == 1)
           {
                   String _name = reader.inputName();
                   currEmployee = personCtr.searchEmployeeByName(_name);
                   _id = currEmployee.getId();         
           }//end if
           else if (choise == 2)
               {
                   _id = reader.inputId();
                   currEmployee = personCtr.searchEmployeeById(_id);
                   }//end If
           
           if(choise == 3)
           {
               exit = true;
           }//end if
           
           if(choise !=3)
           {
                String _name = reader.inputName();
                String _adress = reader.inputAddress();
                String _zipCode = reader.inputPostalCode();
                String _phoneNr = reader.inputPhone();
                String _salaryAccount = reader.inputSalaryAccount();
                int _discount = reader.inputDiscount();


                personCtr.updateEmployee(_id, _name, _adress, _zipCode, _phoneNr, _salaryAccount, _discount);
           
                System.out.println(_name + " er blevet opdateret");
                exit = true;
        }
        }//end if   
   }
   
   /**
    * lists all objects of Employee
    */
   public void listAllEmployee()
   {
       personCtr.returnAllEmployee();
       pause();
   }
   
   /**
    * deletes an object of Employee
    */
   public void deleteEmployee()
   {
       System.out.println("Slet Ansatte ud fra navn eller id?");
       boolean exit = false;
       while(!exit)
       {
           int choise = writeDeleteMenu();
           if(choise == 1)
           {
                   String _name = reader.inputName();
                   personCtr.deleteEmployee(_name);
                   System.out.println("Ansatte slettet");
                   exit = true;
           }//end if
           else
           {
               if(choise == 2)
               {
                   int _id = reader.inputId();
                   personCtr.deleteEmployee(_id);
                   System.out.println("Ansatte slettet");
                   exit = true;
               }//end If
               else
               {
                   if(choise == 3)
                   {
                       exit = true;
                   }//end if
           }//end if
        }//end if   
       }//end while   
   }
   
   /**
    * prints an end message
    */
   private void writeEnd()
   {
        System.out.println(" Systemet slukker ");
   }
   /**
    * takes an input from the user
    */
   private void pause()
   {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
   }
   /**
    * prints a delete message
    */
   private int writeDeleteMenu()
   {
       Scanner keyboard = new Scanner(System.in); 
       System.out.println("Tryk 1 for at slette med navn");
       System.out.println("Tryk 2 for at slette med ID");
       System.out.println("Tryk 3 for at gå tilbage");
        
        int choise = keyboard.nextInt();
        return choise;
   }/**
    * prints a search message
    */
   public int writeSearchMenu()
   {
       Scanner keyboard = new Scanner(System.in); 
       System.out.println("Tryk 1 for at søge med navn");
       System.out.println("Tryk 2 for at søge med ID");
       System.out.println("Tryk 3 for at gå tilbage");
        
       int choise = keyboard.nextInt();
       return choise;
   }
   /**
    * prints an update message
    */
   private int writeUpdateMenu()
   {
       Scanner keyboard = new Scanner(System.in); 
       System.out.println("Tryk 1 for at søge efter navn");
       System.out.println("Tryk 2 for at søge med ID");
       System.out.println("Tryk 3 for at gå tilbage");
        
       int choise = keyboard.nextInt();
       return choise;
   }
   
}
    